package org.example.jcclubsubjectdomain.handler;

import org.example.jcclubsubjectcommon.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 啊昌
 * @date 2025/4/12 19:13
 * SubjectTypeHandlerFactory  题目类型添加工厂
 */

//InitializingBean 接口  Spring 提供的生命周期接口，实现其 afterPropertiesSet() 方法，会在 Bean 的属性注入完成后自动调用。
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {


//	比如 子类实现了 SubjectTypeHandler接口 也添加了@Component 注解  则会自动注入到 subjectTypeHandlerList 中
	@Resource
	private List<SubjectTypeHandler> subjectTypeHandlerList;

	private Map<SubjectInfoTypeEnum, SubjectTypeHandler> map = new HashMap<>();


//	根据不同的题目类型返回对应的处理器
	public SubjectTypeHandler getHandler(Integer subjectInfoType) {
		SubjectInfoTypeEnum infoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectInfoType);
		return  map.get(infoTypeEnum);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		subjectTypeHandlerList.forEach(subjectTypeHandler -> {
			map.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
		});
	}
}
